/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainWindows.java
 *
 * Created on 14-ago-2011, 3.56.26
 */
package agenda;

import agenda.DatabaseManager.SearchLocation;
import agenda.database.ParseException;
import agenda.entityes.AgeEntry;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import javax.swing.AbstractButton;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author emanuele
 */
public class MainWindow extends javax.swing.JFrame {
   /*
    * netbeans rompeva i coglioni ,e gli ho detto di aggiungere sto coso per farlo stare zitto
    */

   private static final long serialVersionUID = 1L;
   /*
    * in questo punto, sempre per via dell'eccessivo colleamento fra GUI e programma sottostante, non posso usare un
    * oggetto di tipo DBManager, perchè quando implementeremo il DB in java sarei costretto a cambiae tutto. Quindi
    * metto una interfaccia.
    */
   //DBManager agenda = new DBManager();
   DatabaseManager<AgeEntry> dbManager;
   private String dbFileName = "ageTestDB";

   /**
    * Entries aggiunte nella sessione corrente.
    */
   List<AgeEntry> sessionEntries = new LinkedList<AgeEntry>();
   /**
    * Possibilità di scrivere nel database
    */
   private boolean editEnabled = true;

   /**
    * Creates new form MainWindows
    *
    * @throws FileNotFoundException
    * @throws ParseException
    * @throws IOException
    */
   public MainWindow() throws FileNotFoundException, ParseException, IOException {
      initComponents();
      initComponents2();

      /*
       * per ora creo una semplice classe anonima per testare il funzionament. poi quando faremo il database, bisognerà
       * implementarla veramente (L'interfaccia DatabaseManager)
       */
      dbManager = new DatabaseManager<AgeEntry>() {

         @Override
         public List<AgeEntry> getAllEntries() {
            return null;
         }

         @Override
         public void addEntry(AgeEntry entry) {
         }

         @Override
         public void openDB(File f) throws FileNotFoundException, java.text.ParseException {
         }

         @Override
         public void closeDB() {
         }

         @Override
         public List<AgeEntry> search(String s, boolean caseSens, SearchLocation loc) {
            return null;
         }

         @Override
         public List<AgeEntry> prefixSearch(String s, boolean caseSens, SearchLocation loc) {
            return null;
         }
      };


      /*
       * crea un sile di database nella directory attuale ... eh , non proprio ma quasi
       */
      File f = new File(dbFileName);
      System.out.println("db file = " + f.getAbsolutePath());
      f.createNewFile();
      try {
         dbManager.openDB(f);
      } catch (java.text.ParseException ex) {
         System.out.println("Errore nel parsing del database");
      }

      pack();
   }

   /*
    * secondo metodo interno per l'inizializzazione dei componenti
    */
   private void initComponents2() {
      // inizializza il listner per i ToggleButton
      MouseListener ml = new MouseAdapter() {
       @Override
         public void mouseClicked(MouseEvent e) {
            updateListView(getToggleButtonString());
         }
      };

      /*
       * imposta il listner in ogni ToggleButton
       */
      Enumeration<AbstractButton> b = buttonGroup1.getElements();
      for (; b.hasMoreElements();) {
         b.nextElement().addMouseListener(ml);
      }
      // seleziona il togglebutton che ha la lettera A
      /*
       * TODO successivamente si potrebbe pensare di fare in modo che venga selezionata la lettera che era selezionata
       * al momento della chiusura della precedente sessione
       */
      jToggleButton1.setSelected(true);

      // inserisci i valori nel combobox di selezione del tipo di ricerca
      for (DatabaseManager.SearchLocation st : DatabaseManager.SearchLocation.values()) {
         srcTypeCB.addItem(st);
      }

      // imposta il window listner per gestire gli eventi della finestra principale
      addWindowListener(new WindowListener() {

         @Override
         public void windowOpened(WindowEvent e) {
            // aggiorna la vista all'apertura della finestra
            updateListView(getToggleButtonString());
         }

         @Override
         public void windowClosing(WindowEvent e) {
            dbManager.closeDB();
         }

         @Override
         public void windowClosed(WindowEvent e) {
         }

         @Override
         public void windowIconified(WindowEvent e) {
         }

         @Override
         public void windowDeiconified(WindowEvent e) {
         }

         @Override
         public void windowActivated(WindowEvent e) {
         }

         @Override
         public void windowDeactivated(WindowEvent e) {
         }
      });


      /*
       * imposta un listner per attivare i bottoni "modiica" ed "elimina, solo quando è selezionata una voce nella lista
       */
      jList1.addListSelectionListener(new ListSelectionListener() {

         @Override
         public void valueChanged(ListSelectionEvent lse) {
            boolean value = jList1.getSelectedIndex() != -1;
            editjButton.setEnabled(value);
            deljButton.setEnabled(value);
         }
      });



   }


   /*
    * restituisce il testo del ToggleButton selezionato nella finestra principale. null se nessuno è selezionato
    */
   private String getToggleButtonString() {
      Enumeration<AbstractButton> b = buttonGroup1.getElements();
      for (; b.hasMoreElements();) {
         AbstractButton x = b.nextElement();
         if (x.isSelected()) {
            return x.getText();
         }
      }
      return null;// non capiterà mai... ma controlliamo comuque
   }

   /**
    * è er main
    *
    * @param args the command line arguments
    */
   public static void main(String args[]) {


      /*
       * Create and display the form
       */

      java.awt.EventQueue.invokeLater(new Runnable() {

         @Override
         public void run() {
            try {
               new MainWindow().setVisible(true);
            } catch (FileNotFoundException ex) {
               JOptionPane.showMessageDialog(null,
                       "File del database non trovato",
                       "Errore", JOptionPane.ERROR_MESSAGE);
            } catch (ParseException ex) {
               JOptionPane.showMessageDialog(null,
                       "Errore parsing del file del database",
                       "Errore", JOptionPane.ERROR_MESSAGE);
            } catch (IOException ex) {
               JOptionPane.showMessageDialog(null,
                       "Errore accesso al file del database",
                       "Errore", JOptionPane.ERROR_MESSAGE);
            }
         }
      });
   }

   /**
    * Aggiorna la lista delle voci che viene visualizzata nella lista.
    *
    *
    *
    * @param s lettera Iniziale del nome
    */
   public void updateListView(String s) {
      if (s == null) {
         throw new IllegalArgumentException();
      }
      boolean showall = showAllCB.isSelected();

      /*
       * se è necessario mostrare tutto esegui questo blocco ed esci
       */
      if (showall) {
         // prendi tutti gli elementi del DB
         List<AgeEntry> list = dbManager.getAllEntries();
         // aggiungici quelli della sessione
         list.addAll(sessionEntries);

         // elimina tutti gli elementi presenti nella lista
         ((DefaultListModel) jList1.getModel()).clear();

         // e poi riempila dinuovo
         for (AgeEntry x : list) {
            ((DefaultListModel) jList1.getModel()).addElement(x);
         }

         return;
      }



      /*
       * se non si devono visualizzare tutti gli elementi esegui la ricerca e mostra i risultati
       */

      // prendi il valore del ComboBox che permette la selezione della locazione dove ricercare
      DatabaseManager.SearchLocation t = (DatabaseManager.SearchLocation) srcTypeCB.getSelectedItem();
      List<AgeEntry> list = dbManager.prefixSearch(s, false, t);

      if (list != null) {
         for (AgeEntry x : list) {
            ((DefaultListModel) jList1.getModel()).addElement(x);
         }
      }
      /*
       * FIXME è necessario un metodo per eseguire la ricerca anche nelle sessionEntries, altrimenti verranno sempre
       * include tutte indipendentemente dal ToggleButton selezionato.
       *
       * ma è meglio fare un petodo di ricercain questo package, o delegare il compito al database e usare un metodo
       * dell'interfaccia per eseguire la ricerca?
       */
      for (AgeEntry x : sessionEntries) {
         ((DefaultListModel) jList1.getModel()).addElement(x);
      }

   }

   /**
    * Abilita o disabilita tutti i toggle button della finestra
    *
    * @param e
    */
   private void togglesSetEnabled(boolean e) {
      Enumeration<AbstractButton> enu = buttonGroup1.getElements();

      for (; enu.hasMoreElements();) {
         enu.nextElement().setEnabled(e);
      }
   }

   /**
    * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
    * content of this method is always regenerated by the Form Editor.
    */
   @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        buttonGroup1 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        jToggleButton1 = new javax.swing.JToggleButton();
        jToggleButton2 = new javax.swing.JToggleButton();
        jToggleButton3 = new javax.swing.JToggleButton();
        jToggleButton4 = new javax.swing.JToggleButton();
        jToggleButton5 = new javax.swing.JToggleButton();
        jToggleButton6 = new javax.swing.JToggleButton();
        jToggleButton7 = new javax.swing.JToggleButton();
        jToggleButton8 = new javax.swing.JToggleButton();
        jToggleButton9 = new javax.swing.JToggleButton();
        jToggleButton10 = new javax.swing.JToggleButton();
        jToggleButton11 = new javax.swing.JToggleButton();
        jToggleButton12 = new javax.swing.JToggleButton();
        jToggleButton13 = new javax.swing.JToggleButton();
        jToggleButton14 = new javax.swing.JToggleButton();
        jToggleButton15 = new javax.swing.JToggleButton();
        jToggleButton16 = new javax.swing.JToggleButton();
        jToggleButton17 = new javax.swing.JToggleButton();
        jToggleButton18 = new javax.swing.JToggleButton();
        jToggleButton19 = new javax.swing.JToggleButton();
        jToggleButton20 = new javax.swing.JToggleButton();
        jToggleButton21 = new javax.swing.JToggleButton();
        jToggleButton22 = new javax.swing.JToggleButton();
        jToggleButton23 = new javax.swing.JToggleButton();
        jToggleButton24 = new javax.swing.JToggleButton();
        jToggleButton25 = new javax.swing.JToggleButton();
        jToggleButton26 = new javax.swing.JToggleButton();
        clearSearchjButton1 = new javax.swing.JButton();
        srcTypeCB = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        addjButton = new javax.swing.JButton();
        deljButton = new javax.swing.JButton();
        searchjButton = new javax.swing.JButton();
        editjButton = new javax.swing.JButton();
        showAllCB = new javax.swing.JCheckBox();
        directSearchCB = new javax.swing.JCheckBox();
        searchjTextField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Agenda");
        setMinimumSize(new java.awt.Dimension(500, 400));
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jList1.setModel(new DefaultListModel());
        jList1.setName("jList1"); // NOI18N
        jScrollPane1.setViewportView(jList1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.2;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        getContentPane().add(jScrollPane1, gridBagConstraints);

        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(new java.awt.GridBagLayout());

        buttonGroup1.add(jToggleButton1);
        jToggleButton1.setText("A");
        jToggleButton1.setName("jToggleButton1"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton1, gridBagConstraints);

        buttonGroup1.add(jToggleButton2);
        jToggleButton2.setText("B");
        jToggleButton2.setName("jToggleButton2"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton2, gridBagConstraints);

        buttonGroup1.add(jToggleButton3);
        jToggleButton3.setText("C");
        jToggleButton3.setName("jToggleButton3"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton3, gridBagConstraints);

        buttonGroup1.add(jToggleButton4);
        jToggleButton4.setText("D");
        jToggleButton4.setName("jToggleButton4"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton4, gridBagConstraints);

        buttonGroup1.add(jToggleButton5);
        jToggleButton5.setText("E");
        jToggleButton5.setName("jToggleButton5"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton5, gridBagConstraints);

        buttonGroup1.add(jToggleButton6);
        jToggleButton6.setText("F");
        jToggleButton6.setName("jToggleButton6"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton6, gridBagConstraints);

        buttonGroup1.add(jToggleButton7);
        jToggleButton7.setText("G");
        jToggleButton7.setName("jToggleButton7"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton7, gridBagConstraints);

        buttonGroup1.add(jToggleButton8);
        jToggleButton8.setText("H");
        jToggleButton8.setName("jToggleButton8"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton8, gridBagConstraints);

        buttonGroup1.add(jToggleButton9);
        jToggleButton9.setText("I");
        jToggleButton9.setName("jToggleButton9"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 6;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton9, gridBagConstraints);

        buttonGroup1.add(jToggleButton10);
        jToggleButton10.setText("J");
        jToggleButton10.setName("jToggleButton10"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 6;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton10, gridBagConstraints);

        buttonGroup1.add(jToggleButton11);
        jToggleButton11.setText("K");
        jToggleButton11.setName("jToggleButton11"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 10;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton11, gridBagConstraints);

        buttonGroup1.add(jToggleButton12);
        jToggleButton12.setText("L");
        jToggleButton12.setName("jToggleButton12"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 11;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton12, gridBagConstraints);

        buttonGroup1.add(jToggleButton13);
        jToggleButton13.setText("M");
        jToggleButton13.setName("jToggleButton13"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 12;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton13, gridBagConstraints);

        buttonGroup1.add(jToggleButton14);
        jToggleButton14.setText("N");
        jToggleButton14.setName("jToggleButton14"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton14, gridBagConstraints);

        buttonGroup1.add(jToggleButton15);
        jToggleButton15.setText("O");
        jToggleButton15.setName("jToggleButton15"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton15, gridBagConstraints);

        buttonGroup1.add(jToggleButton16);
        jToggleButton16.setText("P");
        jToggleButton16.setName("jToggleButton16"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton16, gridBagConstraints);

        buttonGroup1.add(jToggleButton17);
        jToggleButton17.setText("Q");
        jToggleButton17.setName("jToggleButton17"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton17, gridBagConstraints);

        buttonGroup1.add(jToggleButton18);
        jToggleButton18.setText("R");
        jToggleButton18.setName("jToggleButton18"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton18, gridBagConstraints);

        buttonGroup1.add(jToggleButton19);
        jToggleButton19.setText("S");
        jToggleButton19.setName("jToggleButton19"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton19, gridBagConstraints);

        buttonGroup1.add(jToggleButton20);
        jToggleButton20.setText("T");
        jToggleButton20.setName("jToggleButton20"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton20, gridBagConstraints);

        buttonGroup1.add(jToggleButton21);
        jToggleButton21.setText("U");
        jToggleButton21.setName("jToggleButton21"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton21, gridBagConstraints);

        buttonGroup1.add(jToggleButton22);
        jToggleButton22.setText("V");
        jToggleButton22.setName("jToggleButton22"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton22, gridBagConstraints);

        buttonGroup1.add(jToggleButton23);
        jToggleButton23.setText("W");
        jToggleButton23.setName("jToggleButton23"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton23, gridBagConstraints);

        buttonGroup1.add(jToggleButton24);
        jToggleButton24.setText("X");
        jToggleButton24.setName("jToggleButton24"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 10;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton24, gridBagConstraints);

        buttonGroup1.add(jToggleButton25);
        jToggleButton25.setText("Y");
        jToggleButton25.setName("jToggleButton25"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 11;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton25, gridBagConstraints);

        buttonGroup1.add(jToggleButton26);
        jToggleButton26.setText("Z");
        jToggleButton26.setName("jToggleButton26"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 12;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(jToggleButton26, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        getContentPane().add(jPanel1, gridBagConstraints);

        clearSearchjButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/edit_clear_locationbar_ltr.png"))); // NOI18N
        clearSearchjButton1.setMargin(new java.awt.Insets(0, 0, 0, 0));
        clearSearchjButton1.setName("clearSearchjButton1");
        clearSearchjButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                clearSearchTextField(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 0);
        getContentPane().add(clearSearchjButton1, gridBagConstraints);

        srcTypeCB.setName("srcTypeCB"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        getContentPane().add(srcTypeCB, gridBagConstraints);

        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new java.awt.GridBagLayout());

        addjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/list_add_user.png"))); // NOI18N
        addjButton.setText("Aggiungi");
        addjButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        addjButton.setName("addjButton"); // NOI18N
        addjButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                aggiungiVoceRubrica(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 3, 1);
        jPanel2.add(addjButton, gridBagConstraints);

        deljButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/list_remove_user.png"))); // NOI18N
        deljButton.setText("Elimina");
        deljButton.setEnabled(false);
        deljButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        deljButton.setName("deljButton"); // NOI18N
        deljButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                eliminaVoce(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(3, 1, 1, 1);
        jPanel2.add(deljButton, gridBagConstraints);

        searchjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/search_user.png"))); // NOI18N
        searchjButton.setText("Cerca");
        searchjButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        searchjButton.setName("searchjButton"); // NOI18N
        searchjButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cercaVoce(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.PAGE_START;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(3, 1, 1, 1);
        jPanel2.add(searchjButton, gridBagConstraints);

        editjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/edit_user.png"))); // NOI18N
        editjButton.setText("Modifica");
        editjButton.setEnabled(false);
        editjButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        editjButton.setName("editjButton"); // NOI18N
        editjButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                modificaVoce(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(0, 1, 0, 1);
        jPanel2.add(editjButton, gridBagConstraints);

        showAllCB.setText("Mostra tutto");
        showAllCB.setName("showAllCB"); // NOI18N
        showAllCB.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                showAllCBMouseClicked(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel2.add(showAllCB, gridBagConstraints);

        directSearchCB.setText("Ricerca diretta");
        directSearchCB.setName("directSearchCB"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel2.add(directSearchCB, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        getContentPane().add(jPanel2, gridBagConstraints);

        searchjTextField.setText("ricerca");
        searchjTextField.setName("searchjTextField");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        getContentPane().add(searchjTextField, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

   /**
    * Metodo chiamato quando viene cliccato il bottone per aggiungere una voce della rubrica.
    *
    * Questo metodo non deve permettere l'inserimento di una voce se la rubrica è già aperta da un'altra sessione.
    */
    private void aggiungiVoceRubrica(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_aggiungiVoceRubrica
       /*
        * controlla la possibilità di eseguire modifiche al database, e se non è possibile eseguire modifiche,
        * notificalo all'utente
        */
       if (!editEnabled) {
          JOptionPane.showMessageDialog(this, "Il programma è aperto in modatli&agrave; sola lettura\n"
                  + "non &egrave; possibile eseguire modifiche ai dati");
          return;
       }
       AgeEntry e = new InsertDiag(this, true, null, dbManager.getAllEntries()).getData();
       /*
        * in questo punto, appena viene creata una nuova entry, ala aggiungo immediatamente al DB ma se mi mantenessi
        * una lista delle entry create nella sessione attuale e la scrivessi solo all'uscita della sessione, potrei
        * semplificare la gestione dell'apertura di istanze multiple dell'agenda.
        *
        * oltretutto potrei implementre una sorta di IPC per permettere a tutte le sessioni di apportare modifiche al DB
        * e di notificarlo in qualche modo alle altre sessioni
        */
       if (e != null) {
          sessionEntries.add(e);
       }
       updateListView(getToggleButtonString());
    }//GEN-LAST:event_aggiungiVoceRubrica
   /**
    * metodo invocato per modificare una voce della rubrica.
    *
    * @param evt
    */
    private void modificaVoce(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_modificaVoce
       AgeEntry e = (AgeEntry) jList1.getModel().getElementAt(jList1.getSelectedIndex());
       //TODO  InsertDiag()
    }//GEN-LAST:event_modificaVoce

    private void eliminaVoce(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_eliminaVoce
       /*
        * controlla la possibilità di eseguire modifiche al database, e se non è possibile eseguire modifiche,
        * notificalo all'utente
        */
       if (!editEnabled) {
          JOptionPane.showMessageDialog(this, "Il programma è aperto in modatlià sola lettura\n"
                  + "non &egrave; possibile eseguire la modifica");
          return;
       }

       /*
        * prendi l'indice dell'oggetto selezionato nella listadelle voci
        */
       int s = jList1.getSelectedIndex();

       /*
        * se nessuna voce è selezionata esci.
        */
       if (s < 0) {
          return;
       }

       AgeEntry x = (AgeEntry) jList1.getModel().getElementAt(s);

       sessionEntries.remove(x);
       ((DefaultListModel) jList1.getModel()).removeElement(x);
       updateListView(getToggleButtonString());
    }//GEN-LAST:event_eliminaVoce

    private void cercaVoce(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cercaVoce
       new SearchDiag(this, true, dbManager.getAllEntries()).setVisible(true);
    }//GEN-LAST:event_cercaVoce

   /**
    * metodo invocato quando viene cliccato il CheckBox "mostra tutte le voci".
    */
   private void showAllCBMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_showAllCBMouseClicked
      /*
       * se si devono visualizzare tutte le voci disabilita i bottoni del filtro altrimenti riabilitali
       */
      togglesSetEnabled(!showAllCB.isSelected());

      /*
       * aggiorna il contenuto della lista
       */
      updateListView(getToggleButtonString());

   }//GEN-LAST:event_showAllCBMouseClicked

   /**
    * Pulisce il TextField di ricerca.
    *
    * invocato quando si clicca il .... -.- troppo lungo da scrivere ... tanto hai capito
    *
    * @param evt
    */
   private void clearSearchTextField(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_clearSearchTextField
      searchjTextField.setText("");
   }//GEN-LAST:event_clearSearchTextField
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addjButton;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton clearSearchjButton1;
    private javax.swing.JButton deljButton;
    private javax.swing.JCheckBox directSearchCB;
    private javax.swing.JButton editjButton;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToggleButton jToggleButton10;
    private javax.swing.JToggleButton jToggleButton11;
    private javax.swing.JToggleButton jToggleButton12;
    private javax.swing.JToggleButton jToggleButton13;
    private javax.swing.JToggleButton jToggleButton14;
    private javax.swing.JToggleButton jToggleButton15;
    private javax.swing.JToggleButton jToggleButton16;
    private javax.swing.JToggleButton jToggleButton17;
    private javax.swing.JToggleButton jToggleButton18;
    private javax.swing.JToggleButton jToggleButton19;
    private javax.swing.JToggleButton jToggleButton2;
    private javax.swing.JToggleButton jToggleButton20;
    private javax.swing.JToggleButton jToggleButton21;
    private javax.swing.JToggleButton jToggleButton22;
    private javax.swing.JToggleButton jToggleButton23;
    private javax.swing.JToggleButton jToggleButton24;
    private javax.swing.JToggleButton jToggleButton25;
    private javax.swing.JToggleButton jToggleButton26;
    private javax.swing.JToggleButton jToggleButton3;
    private javax.swing.JToggleButton jToggleButton4;
    private javax.swing.JToggleButton jToggleButton5;
    private javax.swing.JToggleButton jToggleButton6;
    private javax.swing.JToggleButton jToggleButton7;
    private javax.swing.JToggleButton jToggleButton8;
    private javax.swing.JToggleButton jToggleButton9;
    private javax.swing.JButton searchjButton;
    private javax.swing.JTextField searchjTextField;
    private javax.swing.JCheckBox showAllCB;
    private javax.swing.JComboBox srcTypeCB;
    // End of variables declaration//GEN-END:variables
}
